var globalCount = 0;
let localckditor = ' ';
$("#fakeloader").fakeLoader({
    timeToHide: 10000, //Time in milliseconds for fakeLoader disappear
    zIndex: 999, // Default zIndex
    spinner: "spinner6", //Options: 'spinner1', 'spinner2', 'spinner3', 'spinner4', 'spinner5', 'spinner6', 'spinner7'
    bgColor: "#fff", //Hex, RGB or RGBA colors
});
setTimeout(function () {
    $('body').css('opacity', '1');
    $('body').attr("class", "gray-bg") //添加样式
}, 300);

$(document).ready(function () {

    var url = window.location.href;
    var url_param = url.split("=")[1];
    //查询出文章类别
    //设置参数，表示查询所有的类别
    var params = {
        "data": "all"
    };
    $.ajax({
        url: '../selectBlogType',
        type: 'post',
        data: params,
        dataType: 'json',
        success: function (data) {
            var data = data.data;
            var typeName = '';
            for (var i = 0; i < data.length; i++) {
                typeName += '<option value="' + data[i].id + '">' + data[i].typeName + '</option>';
            }
            // 初始化数据
            $(".form-horizontal").find('select[name=typeName]').append(typeName);
        },
        error: function () {
            swal("加载类别错误", "请重试操作", "error");
        }
    });
    //初始化富文本
    // $('#summernote').summernote({
    //     height: 400, //初始化默认高度
    //     minHeight: null, //最小高度
    //     maxHeight: null, //最大高度
    //     lang: 'zh-CN', //注意这里，若要设置语言，则需要引入该语言配置js
    //     placeholder: "请在这里写下您的内容",
    //     onImageUpload: function (files, editor, $editable) {
    //         sendFile(files[0], editor, $editable);
    //     }
    // });

    localckditor =  CKEDITOR.replace('Editor1',
        {
            filebrowserImageUploadUrl :"../uploadBlogImages?",
            toolbar : 'Basic',
            uiColor : '#9AB8F3',
            allowedContent: true
        });
    globalCount++;
    returnAllCount();


    //编辑博客
    var param = {
        id: url_param
    }
    $.ajax({
        url: '../selectBlogById',
        type: 'post',
        data: param,
        dataType: 'json',
        success: function (data) {
            var data = data.data;

            //查询成功
            $("#title").val(data.title);
            $("#introduction").val(data.introduction);

            //localckditor.setData(data.content);

            //编辑器状态为ready时，设置数据
            var waitCKEditorReady = function(data) {
                if(localckditor.status == 'ready') {
                    //console.log("进来了0.0")
                    localckditor.setData(data);
                } else {
                    setTimeout(function() {
                        waitCKEditorReady(data);
                    }, 20);
                }
            }

            //延时调用编辑器的数据渲染
            setTimeout(function() {
                waitCKEditorReady(data.content);
                //console.log(data.content)
            }, 50);


            // $("#Editor1").code(data.content);

            $("#keyword").val(data.keyword);
            var imgPath = '<img class="picPath animated fadeInRight"  style="width:100%" alt="封面" title="点击更换封面" src="' + data.images + '" />';
            $(".imagePath").val(data.images);
            $(".picPath").html(imgPath)
            //加载编辑页面预览的  时间和浏览量
            $(".newsview").find(".au02").html(Format(data.addTime, "yyyy-MM-dd hh:mm:ss"));
            $(".au03").find('b').html(data.clickNum);

            $("#clicknum").html(data.clickNum);

            var istop = '否';
            if (data.isTop == 1) {
                istop = '是';
            }
            var isrecommend = '否';
            if (data.isRecommend == 1) {
                isrecommend = '是';
            }
            $("#istop").html(istop);
            $("#isrecommend").html(isrecommend);
            var typeName = '';
            var type = data.typeId;
            var tId = type.split("=")[0];
            var typename = type.split("=")[1];
            typeName += '<option style="color:#000;font-weight:bold;" value="' + tId + '">' + typename + '</option>';
            $(".form-horizontal").find('select[name=typeName]').prepend(typeName);

            $("#typeName option[value=" +tId+ "]").prop("selected", true);


        },
        error: function () {
            swal("查询错误", "请重试操作", "error");
        }
    });
    $("#cy_cmt_count").attr('id','sourceId::'+url_param);
    var p = {
        client_id: 'cyuTAEp5D',
        topic_source_id: url_param
    };
    $.ajax({
        url: 'http://changyan.sohu.com/api/2/topic/count',
        type: 'get',
        data: p,
        dataType: 'jsonp',
        success: function (pl) {
            $("#commentnum").html(pl.result[url_param].comments);
        },
        error: function () {
            swal("获取评论数错误", "请重试操作", "error");
        }
    });
});

var returnAllCount = function () {
    if (globalCount == 1) {
        setTimeout(function () {
            $('#fakeloader').css('display', 'none');
        }, 500);
    }
}

//图片上传  
function sendFile(file, editor, $editable) {
    var filename = false;
    try {
        filename = file['name'];
    } catch (e) {
        filename = false;
    }
    if (!filename) {
        $(".note-alarm").remove();
    }

    //以上防止在图片在编辑器内拖拽引发第二次上传导致的提示错误
    data = new FormData();
    data.append("file", file);
    data.append("key", filename); //唯一性参数
    $.ajax({
        data: data,
        type: "POST",
        url: "../uploadBlogImages",
        dataType: "json",
        cache: false,
        contentType: false,
        processData: false,
        success: function (date) {
            if (date == '') {
                swal("上传失败", "请重试操作", "error");
            } else {
                var path = '图片地址  ' + date.path;
                swal("上传成功", path, "success");
            }
            editor.insertImage($editable, date.path);
        },
        error: function () {
            swal("上传错误", "请重试操作", "error");
        }
    });
}


//弹出模态框 选择图片
var selectImgPath = function (img) {
    $("#pic").modal('hide');
    var imgPath = '<img class="picPath animated fadeInRight"  style="width:100%" alt="封面" title="点击更换封面" src="' + img.src + '" />';
    $(".picPath").html(imgPath)
    $(".imagePath").val(img.src);
};

//查找服务器图库
var findPicList = function () {
    $.ajax({
        //此处使用的是自己封装的JAVA类
        url: "../getFileList",
        type: "POST",
        success: function (data) {
            if (data.code == 0) {
                swal("服务器图库为空", "请上传", "error");
            } else {
                var pics = '';
                for (var i = 0; i < data.data.length; i++) {
                    pics += '<a class="fancybox" href="javascript:void(0);"><img onclick="selectImgPath(this)" style="width: 190px; height: 115px;float:left;margin-right:3px;" alt="image" src="' + data.data[i] + '" /></a>'
                }
            }
            $(".picsList").html(pics);
        },
        error: function (e) {
            swal("获取图片错误", "请检查接口服务", "error");
        }
    });
};


var prevBlog = function () {
    $(".newsview").find(".news_title").html($("#title").val());
    $(".news_about").find(".news_intr").html($("#introduction").val());

    var keyword = '';
    var inputKeyword = $("#keyword").val();
    $(".newsview").find(".tags").html("");
    if (inputKeyword != '' && inputKeyword != null) {
        if (inputKeyword.search(';') != -1 || inputKeyword.search('；') != -1) {
            if (inputKeyword.search('；') != -1) {
                inputKeyword = inputKeyword.replace(/；/g, ";");
            }
            var strs = new Array();
            strs = inputKeyword.split(";");
            for (var i = 0; i < strs.length && strs[i] != ''; i++) {
                keyword += '<a href="#">' + strs[i] + '</a>';
            }
        } else {
            keyword = '<a href="#">' + inputKeyword + '</a>';
        }
    }
    $(".newsview").find(".tags").append(keyword);
    $(".newsview").find(".news_infos").html(localckditor.document.getBody().getHtml());
    var topNum = 1;
    $(".news_infos :header").each(function () {
        $(this).attr("id", 'nav1_' + topNum + '');
        topNum++;
    });
    var update = '<a  class="btn btn-white" href="#" onclick="updateBlog(1)">发表</a>';
    $(".modal-footer").find(".update").html(update);
    $('pre').each(function (i, block) {
        hljs.highlightBlock(block);
    });
}


var updateBlog = function (id) {
    var url = window.location.href;
    var url_param = url.split("=")[1];
    if (url_param.substring(url_param.length,url_param.length -1) == '#'){
        url_param = url_param.substring(0,url_param.length - 1);
    }else {
        url_param = url_param;
    }
    var prarm = '将ID为<span class="text-info">' + url_param + '</span>的博客<span class="text-success">发表</span>';
    if (id == -1) {
        prarm = '将ID为<span class="text-info">' + url_param + '</span>的博客放入<span class="text-navy">草稿箱</span>';
    }
    var params = {
        'id': url_param,
        'title': $("#title").val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;"),
        'introduction': $("#introduction").val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;"),
        'typeId': $("#typeName").val(),
        'images': $(".imagePath").val(),
        'keyword': $("#keyword").val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;"),
        'content': $(".news_infos").html(),
        'status': id,
        prarm: prarm
    };
    $.ajax({
        url: '../updateBlog',
        type: 'post',
        data: params,
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $("#myModal").modal('hide');
                var title = "更新成功";
                var text = "博客已在前端展示";
                if (id == -1) {
                    title = "放入草稿成功";
                    text = "你可以前往草稿箱查看";
                }
                swal({
                    title: title,
                    text: text,
                    type: "success",
                    timer: 1500,
                });
                setTimeout(function () {
                    window.location.href = "../blog/blogTable";
                }, 1500);
            } else if (data.code = 300) {
                swal("对不起", "当前账户没有权限", "error");
            } else {
                swal("更新失败", data.msg, "error");
            }
        },
        error: function () {
            swal("更新错误", "请重新操作", "error");
        }
    });

}

//格式化时间
function Format(datetime, fmt) {
    if (parseInt(datetime) == datetime) {
        if (datetime.length == 10) {
            datetime = parseInt(datetime) * 1000;
        } else if (datetime.length == 13) {
            datetime = parseInt(datetime);
        }
    }
    datetime = new Date(datetime);
    var o = {
        "M+": datetime.getMonth() + 1, //月份
        "d+": datetime.getDate(), //日
        "h+": datetime.getHours(), //小时
        "m+": datetime.getMinutes(), //分
        "s+": datetime.getSeconds(), //秒
        "q+": Math.floor((datetime.getMonth() + 3) / 3), //季度
        "S": datetime.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (datetime.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

//只有验证通过才能执行 添加
$("#add_draft,#add_draft2").click(function () {
    if ($("#commentForm").valid()) {
        $("#prev2").click();
        updateBlog(-1);
    }
});

//只有验证通过才能执行 预览
$("#prev1").click(function () {
    if ($("#commentForm").valid()) {
        $("#prev2").click();
    }
});